<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日志管理API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            border-radius: 5px;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 12px;
        }
        .success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        input, select {
            padding: 8px;
            margin: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 200px;
        }
        .form-group {
            margin: 10px 0;
        }
        label {
            display: inline-block;
            width: 120px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <h1>日志管理API测试</h1>
    
    <div class="container">
        <h2>API基础信息</h2>
        <p><strong>基础URL:</strong> <span id="baseUrl">http://localhost:5095</span></p>
        <p><strong>状态:</strong> <span id="apiStatus">未测试</span></p>
        <button onclick="testApiStatus()">测试API连接</button>
    </div>

    <div class="container">
        <div class="test-section">
            <h3>1. 获取日志列表</h3>
            <div class="form-group">
                <label>页码:</label>
                <input type="number" id="page" value="1" min="1">
            </div>
            <div class="form-group">
                <label>页大小:</label>
                <input type="number" id="pageSize" value="10" min="1" max="100">
            </div>
            <button onclick="testGetLogs()">获取日志列表</button>
            <div id="getLogsResult" class="result"></div>
        </div>

        <div class="test-section">
            <h3>2. 条件查询日志</h3>
            <div class="form-group">
                <label>操作人:</label>
                <input type="text" id="operatorName" placeholder="输入操作人姓名">
            </div>
            <div class="form-group">
                <label>操作类型:</label>
                <select id="operationType">
                    <option value="">全部</option>
                    <option value="LOGIN">登录</option>
                    <option value="LOGOUT">登出</option>
                    <option value="CREATE">创建</option>
                    <option value="UPDATE">更新</option>
                    <option value="DELETE">删除</option>
                    <option value="QUERY">查询</option>
                </select>
            </div>
            <div class="form-group">
                <label>模块:</label>
                <select id="module">
                    <option value="">全部</option>
                    <option value="USER">用户管理</option>
                    <option value="ROLE">角色管理</option>
                    <option value="MENU">菜单管理</option>
                    <option value="PERMISSION">权限管理</option>
                    <option value="SYSTEM">系统设置</option>
                    <option value="FILE">文件管理</option>
                </select>
            </div>
            <button onclick="testQueryLogs()">条件查询</button>
            <div id="queryLogsResult" class="result"></div>
        </div>

        <div class="test-section">
            <h3>3. 获取统计信息</h3>
            <button onclick="testGetStatistics()">获取统计信息</button>
            <div id="getStatisticsResult" class="result"></div>
        </div>

        <div class="test-section">
            <h3>4. 获取最近日志</h3>
            <div class="form-group">
                <label>数量:</label>
                <input type="number" id="recentCount" value="5" min="1" max="50">
            </div>
            <button onclick="testGetRecentLogs()">获取最近日志</button>
            <div id="getRecentLogsResult" class="result"></div>
        </div>

        <div class="test-section">
            <h3>5. 导出日志</h3>
            <div class="form-group">
                <label>格式:</label>
                <select id="exportFormat">
                    <option value="excel">Excel</option>
                    <option value="csv">CSV</option>
                    <option value="json">JSON</option>
                </select>
            </div>
            <button onclick="testExportLogs()">导出日志</button>
            <div id="exportLogsResult" class="result"></div>
        </div>
    </div>

    <script>
        const baseUrl = 'http://localhost:5095';
        
        // 显示结果
        function showResult(elementId, data, type = 'info') {
            const element = document.getElementById(elementId);
            element.className = `result ${type}`;
            element.textContent = typeof data === 'object' ? JSON.stringify(data, null, 2) : data;
        }

        // 测试API连接
        async function testApiStatus() {
            try {
                const response = await fetch(`${baseUrl}/api/operationlog/list?page=1&pageSize=1`);
                if (response.ok) {
                    document.getElementById('apiStatus').textContent = '连接正常';
                    document.getElementById('apiStatus').style.color = 'green';
                    showResult('getLogsResult', 'API连接正常', 'success');
                } else {
                    document.getElementById('apiStatus').textContent = '连接失败';
                    document.getElementById('apiStatus').style.color = 'red';
                    showResult('getLogsResult', `API连接失败: ${response.status}`, 'error');
                }
            } catch (error) {
                document.getElementById('apiStatus').textContent = '连接错误';
                document.getElementById('apiStatus').style.color = 'red';
                showResult('getLogsResult', `API连接错误: ${error.message}`, 'error');
            }
        }

        // 获取日志列表
        async function testGetLogs() {
            try {
                const page = document.getElementById('page').value;
                const pageSize = document.getElementById('pageSize').value;
                
                const response = await fetch(`${baseUrl}/api/operationlog/list?page=${page}&pageSize=${pageSize}`);
                const data = await response.json();
                
                if (response.ok) {
                    showResult('getLogsResult', data, 'success');
                } else {
                    showResult('getLogsResult', data, 'error');
                }
            } catch (error) {
                showResult('getLogsResult', `请求失败: ${error.message}`, 'error');
            }
        }

        // 条件查询日志
        async function testQueryLogs() {
            try {
                const operatorName = document.getElementById('operatorName').value;
                const operationType = document.getElementById('operationType').value;
                const module = document.getElementById('module').value;
                
                let url = `${baseUrl}/api/operationlog/list?page=1&pageSize=10`;
                
                if (operatorName) url += `&operatorName=${encodeURIComponent(operatorName)}`;
                if (operationType) url += `&operationType=${encodeURIComponent(operationType)}`;
                if (module) url += `&module=${encodeURIComponent(module)}`;
                
                const response = await fetch(url);
                const data = await response.json();
                
                if (response.ok) {
                    showResult('queryLogsResult', data, 'success');
                } else {
                    showResult('queryLogsResult', data, 'error');
                }
            } catch (error) {
                showResult('queryLogsResult', `请求失败: ${error.message}`, 'error');
            }
        }

        // 获取统计信息
        async function testGetStatistics() {
            try {
                const response = await fetch(`${baseUrl}/api/operationlog/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    showResult('getStatisticsResult', data, 'success');
                } else {
                    showResult('getStatisticsResult', data, 'error');
                }
            } catch (error) {
                showResult('getStatisticsResult', `请求失败: ${error.message}`, 'error');
            }
        }

        // 获取最近日志
        async function testGetRecentLogs() {
            try {
                const count = document.getElementById('recentCount').value;
                const response = await fetch(`${baseUrl}/api/operationlog/recent?page=1&pageSize=${count}`);
                const data = await response.json();
                
                if (response.ok) {
                    showResult('getRecentLogsResult', data, 'success');
                } else {
                    showResult('getRecentLogsResult', data, 'error');
                }
            } catch (error) {
                showResult('getRecentLogsResult', `请求失败: ${error.message}`, 'error');
            }
        }

        // 导出日志
        async function testExportLogs() {
            try {
                const format = document.getElementById('exportFormat').value;
                
                const response = await fetch(`${baseUrl}/api/operationlog/export`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        format: format,
                        operatorName: '',
                        operationType: '',
                        module: '',
                        startTime: null,
                        endTime: null
                    })
                });
                
                if (response.ok) {
                    if (format === 'json') {
                        const data = await response.json();
                        showResult('exportLogsResult', data, 'success');
                    } else {
                        // 对于Excel和CSV，创建下载链接
                        const blob = await response.blob();
                        const url = window.URL.createObjectURL(blob);
                        const link = document.createElement('a');
                        link.href = url;
                        link.download = `操作日志_${new Date().toISOString().split('T')[0]}.${format === 'excel' ? 'xlsx' : 'csv'}`;
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                        window.URL.revokeObjectURL(url);
                        
                        showResult('exportLogsResult', `${format.toUpperCase()}文件导出成功，已开始下载`, 'success');
                    }
                } else {
                    const data = await response.json();
                    showResult('exportLogsResult', data, 'error');
                }
            } catch (error) {
                showResult('exportLogsResult', `导出失败: ${error.message}`, 'error');
            }
        }

        // 页面加载时自动测试API连接
        window.onload = function() {
            testApiStatus();
        };
    </script>
</body>
</html> 